package com.hfjyz.expert.expert.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.hfjyz.base.action.GridBaseAction;
import com.hfjyz.base.constant.BaseConstant;
import com.hfjyz.base.exception.BaseException;
import com.hfjyz.expert.expert.bean.ExpertBean;
import com.hfjyz.expert.expert.bean.ExpertExcelBean;
import com.hfjyz.expert.expert.bean.ExpertSearchBean;
import com.hfjyz.expert.expert.service.ExpertService;
import com.hfjyz.system.category.bean.CategoryBean;
import com.hfjyz.system.category.service.CategoryService;
import com.hfjyz.system.user.bean.UserBean;

public class ExpertAction extends GridBaseAction {
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 1748589881693831514L;
    private static final Log SYS_LOG = LogFactory.getLog(BaseConstant.SYS_LOG);
    private ExpertService service = null;
    private CategoryService categoryService = null;
    private ExpertBean expert = null;
    private String selectIds = null;
    private String expertId = null;
    private ExpertExcelBean expertDetail = null;

    protected ExpertSearchBean expertSearch = null;

    public ExpertService getService() {
        return service;
    }

    public void setService(ExpertService service) {
        this.service = service;
    }

    public CategoryService getCategoryService() {
        return categoryService;
    }

    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    public ExpertBean getExpert() {
        return expert;
    }

    public void setExpert(ExpertBean expert) {
        this.expert = expert;
    }

    public ExpertExcelBean getExpertDetail() {
        return expertDetail;
    }

    public void setExpertDetail(ExpertExcelBean expertDetail) {
        this.expertDetail = expertDetail;
    }

    public ExpertSearchBean getExpertSearch() {
        return expertSearch;
    }

    public void setExpertSearch(ExpertSearchBean expertSearch) {
        this.expertSearch = expertSearch;
    }

    public String getSelectIds() {
        return selectIds;
    }

    public void setSelectIds(String selectIds) {
        this.selectIds = selectIds;
    }

    public String getExpertId() {
        return expertId;
    }

    public void setExpertId(String expertId) {
        this.expertId = expertId;
    }

    @Override
    protected List<ExpertBean> getBeanList() throws BaseException {
        HttpSession session = ServletActionContext.getRequest().getSession();
        session.setAttribute(BaseConstant.SESSION_CONDITION_KEY, expertSearch);

        return service.findExpert(getSearchCondition(), expertSearch);
    }

    @Override
    protected long getTotalCount() throws BaseException {
        return service.getExpertCount(expertSearch);
    }

    public String save() {
        map = new HashMap<String, Object>();
        try {
            HttpSession session = ServletActionContext.getRequest().getSession();
            UserBean user = (UserBean) session.getAttribute(BaseConstant.SESSION_USER_INFO_KEY);

            service.saveExpert(expert, user.getId());
            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        } catch (Exception e) {
            SYS_LOG.error("发生未知错误！", e);
            map.put("errorMsg", "发生未知错误，请联系管理员！");
            return ERROR;
        }
    }

    public String updateInit() {
        map = new HashMap<String, Object>();
        try {
            Integer id = Integer.valueOf(selectIds);
            expert = service.updateInitExpert(id);
            List<CategoryBean> categoryList = categoryService.findCategory(1);
            map.put("expert", expert);
            map.put("categorys", categoryList);
            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        }
    }

    public String delete() {
        map = new HashMap<String, Object>();
        try {
            String[] idArray = selectIds.split(",");
            List<Integer> idList = new ArrayList<Integer>();
            for (String id : idArray) {
                idList.add(Integer.valueOf(id));
            }
            service.deleteExpert(idList);
            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        } catch (Exception e) {
            SYS_LOG.error("发生未知错误！", e);
            map.put("errorMsg", "发生未知错误，请联系管理员！");
            return ERROR;
        }
    }

    public String showExpertDetail() throws BaseException {
        Integer id = Integer.valueOf(expertId);
        expert = service.updateInitExpert(id);
        expertDetail = new ExpertExcelBean(expert);
        expertDetail.decodeChangeLine();
        HttpSession session = ServletActionContext.getRequest().getSession();
        if (expertDetail.getPhoto() != null && expertDetail.getPhoto().length > 0) {
            session.setAttribute(BaseConstant.SESSION_USER_PHOTO, expertDetail.getPhoto());
        } else {
            session.removeAttribute(BaseConstant.SESSION_USER_PHOTO);
        }
        return SUCCESS;
    }

    public String execute() {
        return SUCCESS;
    }

    public String exportInit() {
        return SUCCESS;
    }
}
